package StudentSystem;

import java.util.ArrayList;
import java.util.Scanner;
public class StudentTest {
    private static final String ADD_STUDENT  = "1";
    private static final String DELETE_STUDENT  = "2";
    private static final String UPDATE_STUDENT  = "3";
    private static final String SEARCH_STUDENT  = "4";
    private static final String EXIT  = "5";
    public static void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        System.out.println("-----------------------欢迎进入本学生管理系统-------------------------");
        loop:while (true) {
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择:");
            String choose = sc.next();
            switch (choose){
                case ADD_STUDENT-> add(list);
                case DELETE_STUDENT-> delete(list);
                case UPDATE_STUDENT-> update(list);
                case SEARCH_STUDENT-> search(list);
                case EXIT-> {
                    System.out.println("退出学生系统成功");
                    break loop;
                }
                default -> System.out.println("无此选项");
            }
        }
    }
    public static void add(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        Student student = new Student();
        while (true) {
            System.out.println("请输入你要添加的学号");
            String id = sc.next();
            if(contains(list,id)){
                System.out.println("你添加的学号已经存在，请重新输入");
            }else {
                student.setId(id);
                break;
            }
        }
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        student.setName(name);
        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        student.setAge(age);
        System.out.println("请输入学生的地址");
        String address = sc.next();
        student.setAddress(address);
        list.add(student);
        System.out.println("添加学生信息成功");
    }
    public static void delete(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学号");
        String id = sc.next();
        int index = getIndex(list, id);
        if(index >= 0){
            list.remove(index);
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败，你输入的学号不存在");
        }
    }
    public static void update(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改学生的学号");
        String id = sc.next();
        int index = getIndex(list,id);
        if(index == -1){
            System.out.println("此学号不存在，修改失败");
            return;
        }
        Student st = list.get(index);
        System.out.println("请输入修改后的姓名");
        String newName = sc.next();
        st.setName(newName);
        System.out.println("请输入修改后的年龄");
        int newAge = sc.nextInt();
        st.setAge(newAge);
        System.out.println("请输入修改后的地址");
        String newAddress = sc.next();
        st.setAddress(newAddress);
        System.out.println("修改信息成功");
    }
    public static void search(ArrayList<Student> list){
        if(list.isEmpty()){
            System.out.println("当前无学生信息,请添加后再查询");
            return;
        }
        System.out.println("id\t\t名字\t\t年龄\t\t地址");
        for (Student student : list) {
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());
        }
    }
    public static boolean contains(ArrayList<Student> list, String id){
        /*for (Student student : list) {
            if(student.getId().equals(id)){
                return true;
            }
        }
        return false;*/
        return getIndex(list,id) >= 0;
    }
    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
